<template>
    <div class="revise-login">
        <div class="tel">
            <div class="item">
                <div class="item-wrapper">
                    <span class="text text-width">原始支付密码</span>
                    <input type="password" maxlength="6" class="input-text input-code" v-model="rawPwd">
                </div>
                <div class="item-wrapper">
                    <span class="text text-width">新支付密码</span>
                    <input type="password" maxlength="6" class="input-text input-code" v-model="nowPwd">
                </div>
                <div class="item-wrapper">
                    <span class="text text-width">确认支付密码</span>
                    <input type="password" maxlength="6" class="input-text input-code" v-model="confirmPwd">
                </div>
                <div class="item-wrapper border-none">
                    <span class="text">验证码</span>
                    <input type="text" maxlength="6" class="input-text input-code" v-model="code"/>
                    <button class="suf_l_inputBtn code" @click="_sendSMS">{{codeText}}</button>
                </div>
            </div>
            <div class="btn" @click="_sendLogin">
                <v-button :text="btnText"></v-button>
            </div>
        </div>
    </div>
</template>

<script>
import Button from '@c/button/button'
import sha256 from 'js-sha256'
import { vpost, sonpost, fpost, gpost, vlink } from '@api/axios'
import { tokenPost, jtpost, gtpost, tokenLink, jtget } from '@api/axiosByToken'

const CODE_TEXT = '发送验证码'

export default {
    name: 'RevisePay',
    data() {
        return {
            codeText: CODE_TEXT, //获得验证码
            rawPwd: '', //原始支付密码
            nowPwd: '', //新支付密码
            confirmPwd: '', //确认支付密码
            code: '', //验证码
            btnText: '确定提交',
            // 模拟手机号
            rawTel: '1236654485',
        }
    },
    components: {
        'v-button': Button
    },
    methods: {
        // 发送短信验证码
        _sendSMS() {
            if (this.codeText === CODE_TEXT) {
                this._beforeSendCheck()
            }
        },
        // 发送前检查
        _beforeSendCheck() {
            let sendData = {
                username: window.localStorage.loginName
            }

            if (sendData.tel === '') {
                this.$Message.warning('请输入手机号码')
                return
            }

            this._sendTime()
            this._doSendSMS(sendData)
        },
        // 发送验证码倒计时
        _sendTime() {
            let time = 60;
            this.smdTime = setInterval(() => {
                if (time === 0) {
                    this.codeText = CODE_TEXT
                    clearInterval(this.smdTime)
                } else {
                    this.codeText = time + 's'
                    time--
                }
            }, 1000)
        },
        // 发送验证码
        _doSendSMS(sendData) {
            this.$store.state.loadding = true
            vpost(vlink.sign.forgetSMSSend, sendData).then(re => {
				this.$store.state.loadding = false
				if (re.status == 0) {
					this.$Message.success('验证码发送成功')
				} else {
					this.$Message.warning(re.msg)
					this.smsBtn = '发送验证码'
					clearInterval(this.smdTime)
				}
			})
        },
        // 发送数据
        _sendLogin() {
            this._sendBeforeCheck()
        },
        // 发送数据前验证
        _sendBeforeCheck() {
            let sendData = {
                userId: window.localStorage.userId,
                primaryPassword: sha256(this.rawPwd),
                newPassword: sha256(this.nowPwd),
                againPassword: sha256(this.confirmPwd),
                msgCode: this.code
            }

            if (this.rawPwd === '') {
                this.$Message.warning('请输入原始支付密码')
                return
            }
            if (this.nowPwd === '') {
                this.$Message.warning('请输入新支付密码')
                return
            }
            if (this.confirmPwd !== this.nowPwd) {
                this.$Message.warning('两次输入不一致，请从新输入')
                return
            }

            if (sendData.code === '') {
                this.$Message.warning('请输入验证码')
                return
            }

            this._doSend(sendData)
        },
        // 发送
        _doSend(sendData) {
            this.$store.state.loadding = true
            tokenPost(tokenLink.user.pay, sendData).then(re => {
                this.$store.state.loadding = false
                if (re.status === 0) {
                    this.$Message.success('修改成功')
                    this.$router.push('/self')
                } else {
                    this.$Message.warning(re.msg)
                }
            })
        }
    }
}
</script>